<?php
/**
 * licenses : MIT
 * creatTime: 2020/5/15
 * author : weblinuxgame
 * filename: RequestParamsTrait.php
 */

namespace App\Core\Traits\Api;

use ArrayAccess;
use Illuminate\Http\Request;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Validator;
use \Illuminate\Contracts\Validation\Validator as Validation;

/**
 * Trait RequestParamsTrait
 * @package App\Core\Traits\Api
 */
trait RequestParamsTrait
{
    protected $attributes = [];
    protected $_excludes = ['attributes', 'request', '_rules', '_array', '_validator', '_excludes'];

    /**
     * @param Request $request
     */
    protected function initialize(Request $request)
    {
        $keys = $this->attributes();
        foreach ($keys as $key) {
            $name = $this->key($key);
            $this->{$key} = $request->get($name, $this->defaults($name));
        }
    }

    /**
     * 获取
     * @return array
     */
    protected function attributes(): array
    {
        if (empty($this->attributes)) {
            $this->attributes = array_keys(get_class_vars(static::class));
        }
        $this->attributes = array_unique($this->attributes);
        if (!empty($this->_excludes)) {
            $index = [];
            foreach ($this->attributes as $i => $key) {
                if (false !== array_search($key, $this->_excludes)) {
                    $index[] = $i;
                }
            }
            if (!empty($index)) {
                Arr::forget($this->attributes, $index);
            }
        }
        return $this->attributes;
    }

    /**
     * 获取请求参数key
     * @param string $key
     * @return string
     */
    protected function key(string $key): string
    {
        $value = $key;
        if (!isset($this->_alias)) {
            return $key;
        }
        if (is_array($this->_alias) || $this->_alias instanceof ArrayAccess) {
            $value = $this->_alias[$key];
        }
        if (empty($value)) {
            return $key;
        }
        return $value;
    }

    /**
     * 获取默认值
     * @param string $key
     * @return mixed|null
     */
    protected function defaults(string $key)
    {
        $value = null;
        if (!isset($this->_defaults)) {
            return null;
        }
        if (is_array($this->_defaults) || $this->_defaults instanceof ArrayAccess) {
            $value = $this->_defaults[$key];
        }
        return $value;
    }

    /**
     * 换成key:value数组
     * @return array
     */
    protected function getVars(): array
    {
        if (isset($this->_array) && !empty($this->_array) && is_array($this->_array)) {
            return $this->_array;
        }
        $keys = $this->attributes();

        $this->_array = [];
        foreach ($keys as $key) {
            if (null == $this->{$key}) {
                continue;
            }
            $this->_array[$key] = $this->{$key};
        }
        return $this->_array;
    }

    /**
     * 验证
     * @return bool
     */
    protected function verify(): bool
    {
        if (empty($this->_rules)) {
            return true;
        }
        if (!isset($this->_validator) || null == $this->_validator) {
            $this->_validator = Validator::make($this->getVars(), $this->_rules);
        }
        return !$this->_validator->fails();
    }

    /**
     * 获取验证器
     * @return Validation|null
     */
    public function validator()
    {
        return $this->_validator;
    }
}
